﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('region.type')+' :'">
          <el-select
            v-model="listQuery.region_type"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('region.type')"
          >
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('region.region')+' :'">
          <el-input
            v-model="listQuery.name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('region.region')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.id')">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('region.countryName')">
        <template slot-scope="scope">
          <span>{{ scope.row.country_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('region.provinceName')">
        <template slot-scope="scope">
          <span>{{ scope.row.province_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('region.cityName')">
        <template slot-scope="scope">
          <span>{{ scope.row.city_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('region.districtName')">
        <template slot-scope="scope">
          <span>{{ scope.row.district_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('region.region')">
        <template slot-scope="scope">
          <span>{{ scope.row.region_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('region.type')">
        <template slot-scope="scope">
          <span>{{ areaTypeEs(scope.row.region_type) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "Region",
  data() {
    return {
      loading: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      typeOptions: [
        {
          value: 0,
          label: "国家"
        },
        {
          value: 1,
          label: "省份"
        },
        {
          value: 2,
          label: "城市"
        },
        {
          value: 3,
          label: "区县"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        name: "",
        region_type: ""
      },
      dialogStatus: "",
      textMap: {
        update: "更新",
        create: "添加",
        review: "审核",
        read: "查看"
      }
    };
  },
  computed: {
    operWidth: function() {
      return this.opreaterItem * 80 + "px";
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page,
        type: 2
      };
      if (this.listQuery.name) {
        data["name"] = this.listQuery.name;
      }
      if (this.listQuery.region_type || this.listQuery.region_type === 0) {
        data["region_type"] = this.listQuery.region_type;
      }
      const regionIndexPath = {
        // 获取区域列表
        company: "STQ",
        api: "pc_region_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(regionIndexPath), res => {
        if (res) {
          if (res !== '{"type":"ping"}') {
            const resj = JSON.parse(res);
            if (resj.code === -1) {
              this.$message.error(resj.message);
            } else {
              this.total = resj.count;
              this.list = resj.data;
            }
          }
          this.listLoading = false;
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        id: undefined,
        name: "",
        region_type: ""
      };
      this.getList();
    }
  }
};
</script>
